﻿var systemType=22;
var code;
var login = {
    init: function() {             
            this.eventBind();//事件绑定  
            $("#txtUserName")[0].focus();
            console.log("are you okay?", $("#txtUserName"));
//            document.getElementById("txtUserName").focus();
    },
    eventBind: function() {
    	//验证码            
        var container = document.getElementById("vCode");
        code = new vCode(container, {
            len: 4,
            lines: 4,
            bgColor: "#fff",
            colors: ["#000", "#000", "#000", "#000", ],
            fontSizeMin: 20,
            fontSizeMax: 24,
            boxBorder:0,
            boxWidth: 110,
            boxHeight: 40
        });
        $(".showcode").hide();
    	$("#btnLogin").bind("click", function() {        	
    		if (!login.checkIsNull()) {
                return;
            }
            if ($(".showcode").css("display")!="none"&&!code.verify(document.getElementById("txtCheckCode").value)) {
            	//弹出消息并清空输入框同时获取焦点
            	baseShowMessage({
                	msg: '&nbsp;&nbsp;&nbsp;&nbsp;验证码错误！&nbsp;&nbsp;&nbsp;&nbsp;',
                	fn:function(){
                		$("#txtCheckCode").focus();
                	}
                });
            	$("input#txtCheckCode").val("");
                code.update();//刷新验证码
                return;
            }
            login.loginSystem();
        });
        $("#txtUserName").bind("keyup", function(event) {
            login.inputOnEnterPress(event, code);
        });
         $("#txtUserPwd").bind("keyup", function(event) {
            login.inputOnEnterPress(event, code);
        });
          $("#txtCheckCode").bind("keyup", function(event) {
            login.inputOnEnterPress(event, code);
        });
    },
    getCharCode: function(event) {
        var charcode = event.charCode;
        if (typeof charcode === "number" && charcode !== 0) {
            return charcode;
        } else {
            return event.keyCode;
        }
    },
    inputOnEnterPress: function(event, code) {
        event = event || window.event;
        if (!event) {
            return;
        }
        var keyCode = login.getCharCode(event);
        if (keyCode === 13) {
        	login.loginSystem();
            if (event.preventDefault) {
                event.preventDefault();
            } else {
                event.returnValue = false;
            }
            if (!login.checkIsNull()) {
                return;
            }
            if ($(".showcode").css("display")!="none"&&!code.verify(document.getElementById("txtCheckCode").value)) {
            	baseShowMessage({
                	msg: '&nbsp;&nbsp;&nbsp;&nbsp;验证码错误！&nbsp;&nbsp;&nbsp;&nbsp;',
                	fn:function(){
                		$("#txtCheckCode").focus();
                	}
                });
            	$("input#txtCheckCode").val("");
                code.update();//刷新验证码
                return;
            }
            login.loginSystem();
        } else {
            login.setCss($(event.target), "");
        }

    },
    loginSystem: function() {
    	console.log($("#txtUserPwd"),$("#txtUserName"),100);
    	var pwd = login.doSM3(document.getElementById("txtUserPwd").value);
        var url = "/"+projectName+"/bfs/user/login";
        var data = {           
            userName: $.trim(document.getElementById("txtUserName").value),
            password: $.trim(pwd)            
        }; 
       $("input").val("");
       ajaxProcess({
           url : url,
           requestType : 'POST',
           data : data,
           successHandle : function(data) {
        	   var page = "system.html?systemType=9999"; //直接进入“系统管理”子系统
        	   page = "desktop.html?systemType="+systemType;//进入“桌面导航页面”，展现所有子系统       
        	   window.location = page;
           },
           resultHandler:function(data){
        	   if(!data.result){
        		   $(".showcode").show();
        		   code.update();//刷新验证码
        		   $(".txt-box").css("height","310px");
        	   }       	   
           }
       });
    },
    //sm3加密
    doSM3 : function(msg) {
		var msgData = CryptoJS.enc.Utf8.parse(msg);

		var md;
		var sm3keycur = new SM3Digest();
		msgData = sm3keycur.GetWords(msgData.toString());
		sm3keycur.BlockUpdate(msgData, 0, msgData.length);
		var c3 = new Array(32);
		sm3keycur.DoFinal(c3, 0);
		var hashHex = sm3keycur.GetHex(c3).toString();
		return hashHex.toUpperCase();
	},
    checkIsNull: function() {
        if (document.getElementById("txtUserName").value === "") {
            login.setCss($("#txtUserName"), "#D22626");
            return false;
        }
        if (document.getElementById("txtUserPwd").value === "") {
            login.setCss($("#txtUserPwd"), "#D22626");
            return false;
        }
        if ($(".showcode").css("display")!="none"&&document.getElementById("txtCheckCode").value === "") {
            login.setCss($("#txtCheckCode"), "#D22626");
            return false;
        }

        return true;
    },
    setCss: function(obj, color) {
        obj.css("backgroundColor", color);
        obj.focus();
    }
};

window.onload=function(){
    login.init();
}
   
